﻿var trunPageObj;//翻页组件对像
var fromEleIds = "#companyCode,#occurDate,#companyName,#settleInvoCode,#ofSaleInvoCode,#settleInvoID,#settleInvoAcctNO";
var isModify;//是否修改
$(initPage);
function initPage() {
	//新增
	$("#btnAdd").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		isModify = false;
		clearValues();
	});
	
	//修改
	$("#btnEdit").click(function() {
		$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", true);
		$("#btnSave,#btnCancle").attr("disabled", false);
		$(fromEleIds).attr("disabled", false);
		isModify = true;
	});
	
	//取消
	$("#btnCancle").click(function() {
		$("#btnEdit,#btnDelete,#btnSave,#btnCancle").attr("disabled", true);
		$("#btnAdd").attr("disabled", false);
		$(fromEleIds).attr("disabled", true);
		clearValues();
	});
	
	//保存
	$("#btnSave").click(function() {
		saveCompanyInfo();
	});
	
	//删除
	$("#btnDelete").click(function() {
		delCompanyInfo();
	});
	
	//初始化翻页组件
    trunPageObj =  $(".pageNext").turnPage();
    
	trunPageObj.gotoPage(1);//搜索第一页
}

//清除表单值
function clearValues() {
	$(fromEleIds).val("");
	$("tr","#data_list").attr("bgColor","#FFFFFF");
}

//保存
function saveCompanyInfo() {
	if($.validInput("companyCode", "公司代码", true, "\<\>\'\"", 8)){
		return false;
	}
	if($.validInput("occurDate", "数据日期", true, "\<\>\'\"", 10)){
		return false;
	}
	if($.validInput("companyName", "公司名称", true, "\<\>\'\"", 120)){
		return false;
	}
	if($.validInput("settleInvoCode", "结算参与人代码", true, "\<\>\'\"", 6)){
		return false;
	}
	if($.validInput("ofSaleInvoCode", "基金销售人代码", true, "\<\>\'\"", 3)){
		return false;
	}
	if($.validInput("settleInvoID", "结算参与人清算编号", true, "\<\>\'\"", 5)){
		return false;
	}
	if($.validInput("settleInvoAcctNO", "结算参与人结算账户", true, "\<\>\'\"", 6)){
		return false;
	}
	var action = isModify ? "modify" : "add";
	$.post("m/company_info/" + action, {
			oldCompanyCode:$.trim($("#oldCompanyCode").val()),
			companyCode:$.trim($("#companyCode").val()),
			occurDate:$.trim($("#occurDate").val()),
			companyName:$.trim($("#companyName").val()),
			settleInvoCode:$.trim($("#settleInvoCode").val()),
			ofSaleInvoCode:$.trim($("#ofSaleInvoCode").val()),
			settleInvoID:$.trim($("#settleInvoID").val()),
			settleInvoAcctNO:$.trim($("#settleInvoAcctNO").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				alert("系统提示：保存成功！");
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

//删除
function delCompanyInfo() {
	if (!confirm("确定要删除记录吗？")) {
		return;
	}
	$.post("m/company_info/delete", {
			companyCode:$.trim($("#oldCompanyCode").val())
		}, function(data) {
			if ($.checkErrorMsg(data) ) {
				$("#btnCancle").click();
				queryList();
			} 
		}, "json");
}

function queryList() {
	var pageno=trunPageObj.getCurrentPageNo();//当前第几页
	var pagecount=trunPageObj.getPageCount();//一页多少条
	$.post("m/company_info/query", {
		pageNo: pageno,
		pageSize : pagecount
	}, function(data) {
		if ($.checkErrorMsg(data) ) {
			var fileList = "";
			if(data.companyInfoPage && data.companyInfoPage.dataList){
				var dataList = data.companyInfoPage.dataList;
				$(dataList).each(function(i) {
					fileList += "<tr class='table_td_font' bgColor='#ffffff' onclick='selectTR(this)'>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + (i + 1) + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].companyCode + "</td>";
					fileList += "<td align='left'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].companyName + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].settleInvoCode + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].ofSaleInvoCode + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].settleInvoID + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].settleInvoAcctNO + "</td>";
					fileList += "<td align='center'  height='25px' class='queryList_linebg' nowrap>" + dataList[i].formatOccurDate + "</td>";
					fileList += "</tr>";
				});
			}
			$("#data_list").html(fileList);
			trunPageObj.setPageData(data.companyInfoPage);
		} 
	}, "json");
}

function selectTR(obj) {
	$(obj).attr("bgColor","#FAEBD7").siblings().attr("bgColor","#FFFFFF");
	var tds = $(obj).find("td");
	$("#companyCode,#oldCompanyCode").val(tds.eq(1).text());
	$("#companyName").val(tds.eq(2).text());
	$("#settleInvoCode").val(tds.eq(3).text());
	$("#ofSaleInvoCode").val(tds.eq(4).text());
	$("#settleInvoID").val(tds.eq(5).text());
	$("#settleInvoAcctNO").val(tds.eq(6).text());
	$("#occurDate").val($.trim(tds.eq(7).text()));
	$("#btnSave,#btnCancle").attr("disabled", true);
	$("#btnAdd,#btnEdit,#btnDelete").attr("disabled", false);
	$(fromEleIds).attr("disabled", true);
}

function changePage(url) {
	if($("#btnSave").attr("disabled") == false) {
		if (!confirm("当前编内容还未保存，是否继续？")) {
			return;
		}
	}
	location.href=BASE_PATH+url;
}